﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using LetsDoIt.Interfaces;
using ResourceManager=LetsDoIt.Localization.ResourceManager;

namespace LetsDoIt.Forms
{
	public partial class SettingsForm : UserControl, INavigationForm
	{

        Dictionary<string, string> listLanguages = new Dictionary<string, string>();

		#region Implementation of INavigationForm

		public MainForm HostForm { get; set; }
		public string Identity { get; set; }
        public bool IsActive { get; set; }

		public void NavigationEnter(object sender)
		{
			//nothing
		    chkUploadDataAsTest.Checked = HostForm.Settings.UploadDataAsTest;
            chkUploadDataAsTest.CheckStateChanged += new EventHandler(chkUploadDataAsTest_CheckStateChanged);
		}

		public void NavigationLeave()
		{
			//nothing
            //save photo settings
            HostForm.Settings.PhotoWidth = (int)txtPhotoWidth.DecimalValue;
            HostForm.Settings.PhotoHeight = (int)txtPhotoHeight.DecimalValue;
            SettingsFactory.SaveSettings(HostForm.Settings);
		}

		#endregion


		#region ctor

        public SettingsForm(MainForm hostForm, string identity)
		{
			HostForm = hostForm;
			Identity = identity;
			InitializeComponent();

            chkUploadDataAsTest.CheckedImage = Properties.Resources.checked_64;
            chkUploadDataAsTest.UncheckedImage = Properties.Resources.unchecked_64;
            btnResetPhotoSize.Image = Properties.Resources.reset_size;
            FillLanguages();
            FillPhotoSettings();
		}

	    private void FillPhotoSettings()
	    {
	        txtPhotoWidth.Text = HostForm.Settings.PhotoWidth.ToString();
            txtPhotoHeight.Text = HostForm.Settings.PhotoHeight.ToString();
	    }

	    private void FillLanguages()
	    {
	        listLanguages.Add("en", "(default english)");
	        listLanguages.Add("ro-RO", "romanian");
            listLanguages.Add("hu-HU", "hungarian");
            //TODO - add multiple language (plug'n play!??!)

	        int selectedIndex = 0;
	        int count = 0;
	        foreach (KeyValuePair<string, string> pair in listLanguages)
	        {
	            cboLanguages.Items.Add(pair.Value);
                if (HostForm.Settings.Language == pair.Key)
                {
                    selectedIndex = count;
                }
	            count++;
	        }
            cboLanguages.SelectedIndex = selectedIndex;

            cboLanguages.SelectedIndexChanged += new EventHandler(cboLanguages_SelectedIndexChanged);
	    }

        void cboLanguages_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
            string selectedValue = cboLanguages.SelectedItem.ToString();
            string culture = "en";
            foreach (KeyValuePair<string, string> pair in listLanguages)
            {
                if (selectedValue == pair.Value)
                    culture = pair.Key;
                    
            }
            //refresh entire application localization
            HostForm.Settings.Language = culture;
            //save language setting
            SettingsFactory.SaveSettings(HostForm.Settings);
            //
            ResourceManager.Instance.Culture = new System.Globalization.CultureInfo(culture);
        }

	    #endregion ctor


		#region events

		private void btnBack_Click(object sender, EventArgs e)
		{
			HostForm.NavigateBack(this, string.Empty);
		}

        void chkUploadDataAsTest_CheckStateChanged(object sender, EventArgs e)
        {
            HostForm.Settings.UploadDataAsTest = chkUploadDataAsTest.Checked;
            SettingsFactory.SaveSettings(HostForm.Settings);
        }

		#endregion events

        private void btnResetPhotoSize_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(
                    ResourceManager.Instance.GetString(@"DialogResetPhotoSizeConfirmation"),
                    ResourceManager.Instance.GetString(@"DialogResetPhotoSizeTitleConfirmation"),
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                txtPhotoWidth.Text = "0";
                txtPhotoHeight.Text = "0";
            }
        }


	}
}
